// pages/my/orderEvaluation/orderEvaluation.js

import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    orderDetails: '',
    userId: 0,
    textValue:'',
    fileList: [],

  },
  afterRead(event) {
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: 'http://localhost:8080/common/upload', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      formData: { user: 'test' },
      success(res) {
        // 上传完成需要更新 fileList
        const { fileList = [] } = this.data;
        fileList.push({ ...file, url: res.data });
        this.setData({ fileList });
      },
    });
    
  },

  insertImage(){
    wx.request({
        url: `http://localhost:8080/hotel/evaluationImages`,
        method: 'post',
        header: {
          Authorization: getApp().globalData.authorization
        },
        data: {
            
        },
        success: (resp) => {
          console.log(resp)
          this.setData({
            orderDetails: resp.data.data
          })
          console.log("ol", this.data.orderDetails)
        }
      })
  },

  onBtn: function (event) {
    this.insertEva()
    this.updateHotelOrder()
    wx.showToast({
      title: '提交成功',
      icon: 'success'
    });
  },

  onChange(event) {
    this.setData({
      value: event.detail,
    });
  },
  Change(e){
    this.setData({
      textValue: e.detail.value,
    });
  },
  getUserId() {
    // 查询用户id
    var loginfo = getApp().globalData.loginfo;
    console.log(loginfo)
    this.setData({
      userId: loginfo.userId
    })
  },
  getHotelOrderById() {
    wx.request({
      url: `http://localhost:8080/hotel/hotelOrder/${this.data.id}`,
      method: 'get',
      header: {
        Authorization: getApp().globalData.authorization
      },
      success: (resp) => {
        console.log(resp)
        this.setData({
          orderDetails: resp.data.data
        })
        console.log("ol", this.data.orderDetails)
      }
    })
  },
  insertEva(){
    wx.request({
      url: 'http://localhost:8080/hotel/hotelEvaluation',
      method: 'post',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        hotelOrderId: this.data.id,
        hotelId: this.data.orderDetails.hotelId,
        evaluationContent: this.data.textValue,
        userId: this.data.userId,
        evaluationScore: this.data.value,
        evaluationTime: 0,

      },
      success: (resp) => {
        console.log(resp)
      }
    })
  },
  updateHotelOrder(){
    wx.request({
      url: 'http://localhost:8080/hotel/hotelOrder',
      method: 'put',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        hotelId: this.data.orderDetails.hotelId,
        userId: this.data.userId,
        status: 0
      },
      success: (resp) => {
        console.log(resp)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("op",options.id)
    this.setData({
      id: options.id
    })
    this.getUserId()
    this.getHotelOrderById()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})